<?php 
class Invoices extends CI_Controller {
	public function __construct() {
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('Jobs');
		$this->load->model('Company');
		$this->load->model('Rinvoices');
	}
	
	public function index() {
		redirect('/');
	}
	
	public function view($cid) {
		$user =& Users::getInstance();
		
		$this->db->where('id', $cid);
		$q = $this->db->get('ainvoices');
		$inv = $q->row();
		
		if ($inv->type == 1) {
			$invoice = $this->Rinvoices->getRI($inv->jid);
			
			$ownerId = $invoice->uid;
		}
		if ($inv->type == 0 || $inv->type == 2) {
			$invoice = $this->Jobs->getOrderJob($inv->jid);
			
			$ownerId = $invoice->owner_id;
		}
		
		if ($user->level > 0 || $invoice->owner_id == $user->id) {
			$this->Jobs->createPDF($user->getUserInfo($ownerId), $invoice, $inv->type, $inv->date, $inv);
		}
	}
	
	public function gen($jid) {
		$this->load->helper('url');
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		
		if ($user->level > 0) {
			// Job info
			$data['jobOrder'] = $this->Jobs->getOrderJob($jid);
			$data['jobRoutes'] = $this->Jobs->getOrderJobs($jid);
			$data['jobUser'] = $user->getUserInfo($data['jobOrder']->owner_id);
			
			if ($this->input->post('issue')) {
				$fields = $this->input->post('fname');
				$values = $this->input->post('fval');
				$aFields = array();
				foreach ($fields as $i => $field) {
					$aFields[] = array('name' => $field, 'value' => $values[$i]);
				}
				$jsonFields = json_encode($aFields);
				
				// Add invoice
				$this->Jobs->addInvoice($jid, 0, $jsonFields, 0);
				
				redirect('/joborder/'.$jid);
			}
			
			$data['user'] = $user;
			$data['due'] = $this->Jobs->getDuePriceList();
			$data['overdue'] = $this->Jobs->getOverduePriceList();
			
			$totalOverdue = 0;
			foreach ($data['overdue'] as $jobOrder) {
				$jobOrder = $jobOrder['order'];
				$totalOverdue += $jobOrder->price - $jobOrder->paid;
			}
			$data['overduetotal'] = $totalOverdue;
			
			$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
			$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
			
			$this->load->view('geninvoice', $data);
		} else {
			redirect('/');
		}
	}
	
	public function l($proforma = "") {
		$this->load->helper('url');
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		
		if ($user->level > 0) {
			$data['user'] = $user;
			$data['due'] = $this->Jobs->getDuePriceList();
			$data['overdue'] = $this->Jobs->getOverduePriceList();
			
			$totalOverdue = 0;
			foreach ($data['overdue'] as $jobOrder) {
				$jobOrder = $jobOrder['order'];
				$totalOverdue += $jobOrder->price - $jobOrder->paid;
			}
			$data['overduetotal'] = $totalOverdue;
			
			$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
			$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
			
			$proforma = (empty($proforma)) ? false : true;
			$data['invoices'] = $this->Jobs->getInvoices($proforma);
//			echo "<pre>";print_r($data['invoices']);die;
			if ($proforma) {
				$data['pageTitle'] = 'Proforma Invoices';
			} else {
				$data['pageTitle'] = 'Invoices';
			}
			
			$this->load->view('invoices-list', $data);
		} else {
			redirect('/');
		}
	}
}
?>